package barbershop;

import java.util.LinkedList;

public class BarberShopConcrete implements BarberShop {
	private LinkedList<Client> queue;
	private int maxlength;

	public BarberShopConcrete(int maxLength) {
		queue = new LinkedList<Client>();
		this.maxlength = maxLength;
	}
	
	@Override
	public void runSingleStep() { // True if barber is working
		if (!queue.isEmpty()) {
			if (queue.get(0).timeLeft()!=0) {
				queue.get(0).clientShave();
				System.out.println(queue.get(0).getName());
			
				for (int a=1;a<queue.size();a++) {
					queue.get(a).clientWait();
				}
			}
			// Client goes away
			else {
				queue.remove(0);
				if (queue.size()==0)System.out.println("Barber gonna sleep");
			}
		}
	}

	@Override
	public boolean clientArrives(Client c) {
		if (queue.size()<maxlength) {
			if (queue.size()==0) System.out.println("Barber wakes up");
			queue.add(c);
			return true;
		}
		else return false;
	}

	@Override
	public int getQueueLength() {
		return queue.size();
	}

	@Override
	public boolean barberWorking() {
		return queue.size()>0;
	}

}
